﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.Media;

namespace DoAnHuongRung
{
    public partial class frmNguyenVatLieu : Form
    {
        public frmNguyenVatLieu()
        {
            InitializeComponent();
        }

        private void frmNguyenVatLieu_Load(object sender, EventArgs e)
        {
            LoadNguyenVatLieu();
            LoadDataTxtTimkiem();
        }

        private void LoadNguyenVatLieu()
        {
            dtgNguyenVatLieu.AutoGenerateColumns = false;
            dtgNguyenVatLieu.DataSource = NguyenVatLieuBUS.SelectAll();
        }

        private void txtTimKiemNCC_Click(object sender, EventArgs e)
        {
            if (txtTimKiem.Text.Equals("Tìm kiếm"))
            {
                txtTimKiem.Text = "";
                txtTimKiem.ForeColor = Color.Black;
            }
        }

        private void LoadDataTxtTimkiem()
        {
            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            List<NguyenVatLieuDTO> list = NguyenVatLieuBUS.SelectAll();
            for (int i = 0; i < list.Count(); i++)
                auto.Add(list[i].TenNVL);
            txtTimKiem.AutoCompleteCustomSource = auto;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                NguyenVatLieuDTO nvl = new NguyenVatLieuDTO();
                nvl.TenNVL = txtTenNVL.Text;
                nvl.DonViTinh = txtDonViTinh.Text;
                bool b = NguyenVatLieuBUS.Insert(nvl);
                if (b == true)
                {
                    SoundPlayer s = new SoundPlayer(Properties.Resources.Windows_Notify);
                    s.Play();
                    LoadNguyenVatLieu();
                    MessageBox.Show("Đã thêm nguyên vật liệu !!!");
                }
            }
            catch (Exception ex)
            {
                SoundPlayer s = new SoundPlayer(Properties.Resources.Windows_Error);
                s.Play();
                MessageBox.Show(ex.Message);
            }
                
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            LoadNguyenVatLieu();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                NguyenVatLieuDTO nvl = new NguyenVatLieuDTO();
                nvl.MaNVL = int.Parse(dtgNguyenVatLieu.CurrentRow.Cells[0].Value.ToString());
                nvl.TenNVL = txtTenNVL.Text;
                nvl.DonViTinh = txtDonViTinh.Text;
                bool b = NguyenVatLieuBUS.Update(nvl);
                if (b == true)
                {
                    SoundPlayer s = new SoundPlayer(Properties.Resources.Windows_Notify);
                    s.Play();
                    LoadNguyenVatLieu();
                    MessageBox.Show("Đã cập nhật nguyên vật liệu !!!");
                }
            }
            catch (Exception ex)
            {
                SoundPlayer s = new SoundPlayer(Properties.Resources.Windows_Error);
                s.Play();
                MessageBox.Show(ex.Message);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                bool b = NguyenVatLieuBUS.Delete(int.Parse(dtgNguyenVatLieu.CurrentRow.Cells[0].Value.ToString()));
                if (b == true)
                {
                    SoundPlayer s = new SoundPlayer(Properties.Resources.Windows_Notify);
                    s.Play();
                    LoadNguyenVatLieu();
                    MessageBox.Show("Đã cập nhật nguyên vật liệu !!!");
                }
            }
            catch (Exception ex)
            {
                SoundPlayer s = new SoundPlayer(Properties.Resources.Windows_Error);
                s.Play();
                MessageBox.Show(ex.Message);
            }
        }

        private void dtgNguyenVatLieu_SelectionChanged(object sender, EventArgs e)
        {
            txtTenNVL.Text = dtgNguyenVatLieu.CurrentRow.Cells[1].Value.ToString();
            txtDonViTinh.Text = dtgNguyenVatLieu.CurrentRow.Cells[2].Value.ToString();
        }

        private void txtTenNVL_Click(object sender, EventArgs e)
        {
            txtTenNVL.Text = "";
        }

        private void txtDonViTinh_Click(object sender, EventArgs e)
        {
            txtDonViTinh.Text = "";
        }
    }
}
